package com.yinhai.msg.thirdPlatform.service.read.impl;

import com.yinhai.msg.thirdPlatform.entity.MsgThirdPlatformPo;
import com.yinhai.msg.thirdPlatform.mapper.read.MsgThirdPlatformReadMapper;
import com.yinhai.msg.thirdPlatform.service.read.MsgThirdPlatformReadService;
import com.yinhai.msg.thirdPlatform.vo.MsgThirdPlatformVo;
import com.yinhai.ta404.core.service.BaseService;
import com.yinhai.ta404.core.transaction.annotation.TaTransactional;
import com.yinhai.ta404.core.utils.ValidateUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhongrd
 * @file 第三方配置管理平台只读service
 * @date 2019/5/20 17:13
 */
@Service("msgThirdPlatformReadService")
@TaTransactional(readOnly = true)
public class MsgThirdPlatformReadServiceImpl extends BaseService implements MsgThirdPlatformReadService {
    @Resource
    private MsgThirdPlatformReadMapper msgThirdPlatformReadMapper;

    /**
     * 获取第三方平台数据
     * 可传入所属平台为参数,若不传则查询所有记录
     * @param map
     * @return List<Map<String,Object>>
     */
    @Override
    public List<Map<String,Object>> queryThirdPlatForm(Map map) {
        List<MsgThirdPlatformPo> msgThirdPlatformPos = msgThirdPlatformReadMapper.query(map);
        //循环判断当前有多少种平台
        List<Map<String,Object>> mapList = new ArrayList<>();
        List<String> list = new ArrayList<>();
        for(int i=0;i<msgThirdPlatformPos.size();i++){
            boolean key = false;
            if(i==0){
                list.add(msgThirdPlatformPos.get(i).getPlatformname());
            }else{
                for(String str: list){
                    if(ValidateUtil.areEqual(str,msgThirdPlatformPos.get(i).getPlatformname())){
                        key=true;
                    }
                }
                if(!key){
                    list.add(msgThirdPlatformPos.get(i).getPlatformname());
                }
            }
        }

        //已所属平台为key,相同所属平台数据为value,组装数据
        for(String str:list){
            List<MsgThirdPlatformVo> msgThirdPlatformVos = new ArrayList<>();
            for (MsgThirdPlatformPo msgThirdPlatformPo : msgThirdPlatformPos){
                if(ValidateUtil.areEqual(str,msgThirdPlatformPo.getPlatformname())){
                    msgThirdPlatformVos.add(msgThirdPlatformPo.toVo(new MsgThirdPlatformVo()));
                }
            }
            Map<String,Object> map1 = new HashMap<>();
            map1.put("list",msgThirdPlatformVos);
            map1.put("type",str);
            mapList.add(map1);
        }
        return mapList;
    }
}
